package com.desire.controller;

import com.desire.common.DateUtil;
import com.desire.common.Result;
import com.desire.common.ResultUtil;
import com.desire.entity.UserEntity;
import com.desire.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.desire.common.ResultEnum.*;

/**
 * (D_Users)表控制层
 */
@Api(tags = "用户操作接口")
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private IUserService userService;

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")
//    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int",defaultValue = "1")
    @GetMapping("/{id}")
    public Result<Object> getUser(@PathVariable Integer id) {
        UserEntity user = userService.findUserById(id);
        if (user != null) {
            user.setFoundTime(DateUtil.timeStamp2Date(user.getFoundTime(), null));
            return ResultUtil.success(SUCCESS, user);
        } else {
            return ResultUtil.error(USER_NOT_FOND);
        }
    }

    /**
     * 获取用户信息，使用PageHelper进行分页
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取用户信息", notes = "分页获取用户信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int",defaultValue = "1"),
//            @ApiImplicitParam(name = "pageSize", value = "每页数", required = true, dataType = "int",defaultValue = "5")
//    })
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<Object> getUsers(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(pageNum, pageSize);
        List<UserEntity> users = userService.findUserAll();
        for (UserEntity user : users) {
            user.setFoundTime(DateUtil.timeStamp2Date(user.getFoundTime(), null));
        }
        PageInfo<UserEntity> pageInfo = new PageInfo<>(users);
        return ResultUtil.success(SUCCESS, pageInfo);
    }

    /**
     * 校验登录
     *
     * @param userEntity
     * @return
     */
    @ApiOperation(value = "校验登录信息是否正确", notes = "根据用户名和密码校验登录")
    @PostMapping("/checkLogin")
    public Result<Object> checkLogin(UserEntity userEntity) {
        UserEntity user = userService.findUserForUsernameAndPwd(userEntity);
        if (user != null) {
            return ResultUtil.success(SUCCESS, user);
        } else {
            return ResultUtil.error(INPUT_ERROR);
        }
    }

    /**
     * 添加用户
     *
     * @param userEntity
     * @return
     */
    @ApiOperation(value = "添加用户", notes = "添加用户，只需用户名、密码、用户状态 三个参数即可")
    @PostMapping("/")
    public Result<Object> addUser(UserEntity userEntity) {
        userEntity.setFoundTime(DateUtil.timeStamp());
        int result = userService.addUser(userEntity);
        if (result > 0) {
            return ResultUtil.success(SUCCESS, userEntity);
        } else {
            return ResultUtil.error(ADD_USER_ERROR);
        }
    }

    /**
     * 根据id修改用户信息
     *
     * @param userEntity
     * @return
     */
    @ApiOperation(value = "根据用户id修改用户", notes = "根据用户id修改用户")
    @PutMapping("/")
    public Result<Object> editUser(UserEntity userEntity) {
        userEntity.setFoundTime(DateUtil.timeStamp());
//        TODO:修改用户
        int result = userService.editUser(userEntity);
        if (result > 0) {
            return ResultUtil.success(UPDATE_SUCCESS, userEntity);
        } else {
            return ResultUtil.error(UPDATE_ERROR);
        }
    }

    @ApiOperation(value = "根据用户id删除用户", notes = "根据用户id删除用户")
    @DeleteMapping(value = "/{id}")
//    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int",defaultValue = "1")
    public Result<Object> deleteUser(@PathVariable Integer id) {
        int result = userService.deleteUser(id);
        if (result > 0) {
            return ResultUtil.success(DELETE_SUCCESS, id);
        } else {
            return ResultUtil.error(DELETE_ERROR);
        }
    }
}
